package com.itheima.controller;

import com.itheima.common.Com.Employee;
import com.itheima.common.pojo.Result;
import com.itheima.common.vo.EmployeeVo;
import com.itheima.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 登录
     * @param employee 从service层传回来的数据
     * @return 登录是否成功
     */
    @PostMapping("login")
    public Result login(@RequestBody Employee employee){
        //获得用户名和密码
        EmployeeVo login = employeeService.login(employee.getUsername(), employee.getPassword());
        //判断是否为空
        if (login==null){
            return Result.error("登入失败，请输入正确的密码或者账号");
        }

        //返回是否成功
        return Result.success(login);
    }

    /**
     * 给前端返回是否访问成功
     * @return
     */
    @GetMapping()
    public Result getEmployee(){
        return Result.success("访问成功");
    }

    /**
     * 登出
     * @param request 接收
     * @return 登出的结果
     */
    @PostMapping("/logout")
    //                       请求方法
    public Result logout(HttpServletRequest request){
        String authorization = request.getHeader("Authorization");

        redisTemplate.delete("token"+authorization);
        employeeService.logout(authorization);
        return Result.success("登出成功");
    }

    /**
     * 添加
     * @param employee 对象
     * @return 新增成功
     */
   @PostMapping("")
    public Result save(@RequestBody Employee employee){
       if (employee.getUsername()==null||employee.getPhone()==null){
           return Result.error("不能为空哦");
       }
       if (employee.getName()==null||employee.getName().trim().length()==0){
           return Result.error("名字不能为空噢");
       }
        employeeService.add(employee);
       return Result.success("新增成功");
   }

    /**
     * 通过id查询
     * @param id 前端传过来的数据
     * @return 查询结果
     */
   @GetMapping("/{id}")
    public Result getById(@PathVariable("id") String id){
        return Result.success(employeeService.getById(id));
   }

    /**
     * 分页查询
     * @param name 真实姓名
     * @param page 查询起始页数
     * @param pageSize 查询的页数
     * @return 查询的集合
     */
   @GetMapping("/page")
    public Result page(String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize){

        return Result.success(employeeService.page(name, page, pageSize));
   }

    /**
     * 修改
     * @param employee 对象
     * @return 修改成功
     */
   @PutMapping("")
    public Result update(@RequestBody Employee employee){

       employeeService.update(employee);
       return Result.success("修改成功");
   }
//   @GetMapping("/{username}")
//    public Result getByUsername(@PathVariable("username") String username){
//       employeeService.getByUsername(username);
//       return Result.success("获取成功");
//   }

}
